using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public enum SlotType { BAG,WEAPON,ARMOR,ACTION,SHOP}
public class SlotHolder : MonoBehaviour,IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler
{
    public SlotType slotType;
    public ItemUI itemUI;


    private void OnDisable()
    {
        InventoryManager.Instance.itemToolTip.gameObject.SetActive(false);
    }

    public void updateItem()
    {
        switch (slotType)
        {
            case SlotType.BAG:
                itemUI.Bag = InventoryManager.Instance.bagData;
                break;
            case SlotType.WEAPON:
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
                {
                    Debug.Log("SaberBag");
                    itemUI.Bag = InventoryManager.Instance.equipmentDataSaber;
                }
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
                {
                    Debug.Log("ArcherBag");
                    itemUI.Bag = InventoryManager.Instance.equipmentDataArcher;
                }
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
                {
                    Debug.Log("MageBag");
                    itemUI.Bag = InventoryManager.Instance.equipmentDataMage;
                }
                if (itemUI.Bag.items[itemUI.Index].itemData!= null)
                {
                    GameManager.Instance.playerStats.attackData.ApplyWeaponData(itemUI.Bag.items[itemUI.Index].itemData.weaponData);
                }
                break;
            case SlotType.ARMOR:
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
                {
                    itemUI.Bag = InventoryManager.Instance.equipmentDataSaber;
                }
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
                {
                    itemUI.Bag = InventoryManager.Instance.equipmentDataArcher;
                }
                if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
                {
                    itemUI.Bag = InventoryManager.Instance.equipmentDataMage;
                }
                break;
            case SlotType.ACTION:
                itemUI.Bag = InventoryManager.Instance.actionData;
                break;
            case SlotType.SHOP:
                itemUI.Bag = InventoryManager.Instance.shopData;
                break;
        }
        var item = itemUI.Bag.items[itemUI.Index];
        itemUI.SetupItemUI(item.itemData, item.amount);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (slotType != SlotType.SHOP)
        {
            if (eventData.clickCount % 2 == 0)
            {
                UseItem();
            }
        }

        else if (slotType == SlotType.SHOP)
        {
            var targetHolder = eventData.pointerClick.gameObject.GetComponent<SlotHolder>();
            var targetItem = targetHolder.itemUI.Bag.items[targetHolder.itemUI.Index];
            InventoryManager.Instance.buyTip.gameObject.SetActive(true);
            InventoryManager.Instance.buyTip.targetItem = targetItem;

        }
    }

    public void UseItem()
    {
        if (itemUI.GetInventoryItem().itemData != null)
        {
            if (itemUI.GetInventoryItem().itemData.itemType == ItemType.Useable && itemUI.GetInventoryItem().amount > 0)
            {
                GameManager.Instance.playerStats.ApplayToHealth(itemUI.GetInventoryItem().itemData.UseableItemData
                    .healthPoint);
                GameManager.Instance.playerStats.AddMoney(itemUI.GetInventoryItem().itemData.UseableItemData.money);
                itemUI.GetInventoryItem().amount -= 1;
                QuestManager.Instance.updateQuestProgress(itemUI.GetInventoryItem().itemData.itemName,-1);
            }
            updateItem();
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (InventoryManager.Instance.buyTip.gameObject.activeSelf)
        {
            InventoryManager.Instance.buyTip.gameObject.SetActive(false);
        }
        if (itemUI.GetInventoryItem().itemData)
        {
            if (slotType == SlotType.SHOP)
            {
                InventoryManager.Instance.itemToolTip.SetUpTooltipForShop(itemUI.GetInventoryItem().itemData);
                InventoryManager.Instance.itemToolTip.gameObject.SetActive(true);
            }
            else
            {
                InventoryManager.Instance.itemToolTip.SetUpTooltip(itemUI.GetInventoryItem().itemData);
                InventoryManager.Instance.itemToolTip.gameObject.SetActive(true);
            }
        }
    }
    
   

    public void OnPointerExit(PointerEventData eventData)
    {
        InventoryManager.Instance.itemToolTip.gameObject.SetActive(false);
    }
}
